package com.shycloud.mido.organ.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shycloud.mido.common.core.constant.CommonConstants;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.organ.api.entity.*;
import com.shycloud.mido.organ.api.vo.OfficeStudentDetailVO;
import com.shycloud.mido.organ.api.vo.OfficeVO;
import com.shycloud.mido.organ.mapper.MemberMapper;
import com.shycloud.mido.organ.mapper.OrganizationMapper;
import com.shycloud.mido.organ.mapper.StudentDetailedMapper;
import com.shycloud.mido.organ.mapper.TeacherStudentMapper;
import com.shycloud.mido.organ.service.OfficeService;
import com.shycloud.mido.organ.service.TeacherDetailedService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @author nianhua.jiang
 * @date 2020/7/24 17:02
 */
@Service
@AllArgsConstructor
public class OfficeServiceImpl extends ServiceImpl<OrganizationMapper, Organization> implements OfficeService {

	/** 工作室 mapper */
	private OrganizationMapper officeMapper;
	/** 学生 mapper */
	private StudentDetailedMapper studentDetailedMapper;
	/** 会员 mapper */
	private MemberMapper memberMapper;
	/** 学生与教师绑定关系 mapper */
	private TeacherStudentMapper teacherStudentMapper;
	/** 教师 mapper */
	private TeacherDetailedService teacherDetailedService;

	/**
	 * 分页获取工作室信息
	 *
	 * @param page
	 * @param officeName
	 * @param teacherName
	 * @return java.util.List<com.shycloud.mido.organ.api.vo.OfficeVO>
	 * @author nianhua.jiang
	 * @date 2020/6/3 10:37
	 */
	@Override
	public IPage<OfficeVO> getOffice(Page page, String officeName, String teacherName) {
		return officeMapper.getOffice(page, officeName, teacherName);
	}

	/**
	 * 查询工作室学生数量
	 *
	 * @param
	 * @return int
	 * @author nianhua.jiang
	 * @date 2020/6/4 9:47
	 */
	@Override
	public int getOfficeStudentCount() {
		return officeMapper.getOfficeStudentCount();
	}

	/**
	 * 查询工作室下的学生信息
	 *
	 * @param page
	 * @param phone
	 * @param studentName
	 * @param officeId
	 * @return com.baomidou.mybatisplus.core.metadata.IPage<com.shycloud.mido.organ.api.vo.OfficeStudentDetailVO>
	 * @author nianhua.jiang
	 * @date 2020/6/4 13:32
	 */
	@Override
	public IPage<OfficeStudentDetailVO> getOfficeStudentDetail(Page page, String phone, String studentName, String officeId) {
		return officeMapper.getOfficeStudentDetail(page, phone, studentName, officeId);
	}

	/**
	 * 批量移除工作室的学生
	 *
	 * @param officeId
	 * @param studentIdList
	 * @return java.lang.Boolean
	 * @author nianhua.jiang
	 * @date 2020/6/4 14:59
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean removeofficeStudent(Integer officeId, List<String> studentIdList) {

		//1.移除学生，将学生详情表删除【逻辑删】
		officeMapper.removeofficeStudent(officeId, studentIdList);
		//2.删除教师与学生的绑定关系
		for (String studentId : studentIdList) {
			teacherStudentMapper.delete(new QueryWrapper<TeacherStudentEntity>().eq("student_id", studentId));
		}

		//3.移除教师与学生的关联分组表
		officeMapper.removeTeacherGroup(studentIdList);

		return true;
	}

	/**
	 * 查询学生
	 *
	 * @param name
	 * @param phone
	 * @return com.shycloud.mido.organ.api.vo.OfficeStudentDetailVO
	 * @author nianhua.jiang
	 * @date 2020/6/4 16:46
	 */
	@Override
	public List<OfficeStudentDetailVO> getStudent(String name, String phone) {
		return officeMapper.getStudent(name, phone);
	}

	/**
	 * 批量添加学生
	 *
	 * @param request
	 * @return java.lang.Boolean
	 * @author nianhua.jiang
	 * @date 2020/6/4 17:09
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public R addStudent(Map<String, Object> request) {

		//memberId集合
		List<String> memberIdList = (List<String>) request.get("memberIdList");
		//工作室ID
		Integer officeId = (Integer) request.get("officeId");

		//循环查询此用户是否已经存在于机构中，如果存在，移出至memberIdList集合外
		for (String memberId : memberIdList) {

			//通过member_id查询此学生是否在此工作室中
			QueryWrapper<StudentDetailedEntity> queryWrapper = new QueryWrapper();
			queryWrapper.eq("member_id", memberId);
			queryWrapper.eq("del_flag", "0");
			//queryWrapper.eq("org_id", officeId);

			//业务需要只能查出一条数据
			StudentDetailedEntity studentDetailed = studentDetailedMapper.selectOne(queryWrapper);

			//根据工作室ID查询教师信息
			TeacherDetailedEntity teacher = teacherDetailedService.getOne(new QueryWrapper<TeacherDetailedEntity>().eq("org_id", officeId));

			if (teacher == null) {
				return new R(CommonConstants.FAIL, "教师信息获取失败", false);
			}

			//如果此用户不是此工作室的学生，那么执行添加操作
			if (studentDetailed == null) {

				//获取会员信息
				MemberEntity member = memberMapper.selectById(memberId);
				StudentDetailedEntity student = new StudentDetailedEntity();
				//工作室ID
				student.setOrgId(officeId);
				//会员Id
				student.setMemberId(memberId);
				//逻辑删除
				student.setDelFlag(0);
				//状态：正式
				student.setStatus("0");
				//学生昵称
				student.setName(member.getShowname());
				//新增
				studentDetailedMapper.insert(student);

				//新增教师-学生 关系表
				TeacherStudentEntity teacherStudentEntity = new TeacherStudentEntity();
				//机构ID
				teacherStudentEntity.setOrgId(officeId);
				//学生ID
				teacherStudentEntity.setStudentId(student.getId());
				//教师ID
				teacherStudentEntity.setTeacherId(teacher.getId());
				//增加师生关系创建时间
				teacherStudentEntity.setCreateTime(LocalDateTime.now());

				teacherStudentMapper.insert(teacherStudentEntity);

			} else if (studentDetailed.getOrgId() == 0) {

				//获取会员信息
				MemberEntity member = memberMapper.selectById(memberId);
				//更新会员信息
				member.setOrgId(officeId);
				memberMapper.updateById(member);

				//工作室ID
				studentDetailed.setOrgId(officeId);
				//新增
				studentDetailedMapper.updateById(studentDetailed);

				//新增教师-学生 关系表
				TeacherStudentEntity teacherStudentEntity = new TeacherStudentEntity();
				//机构ID
				teacherStudentEntity.setOrgId(officeId);
				//学生ID
				teacherStudentEntity.setStudentId(studentDetailed.getId());
				//教师ID
				teacherStudentEntity.setTeacherId(teacher.getId());
				//增加师生关系创建时间
				teacherStudentEntity.setCreateTime(LocalDateTime.now());

				teacherStudentMapper.insert(teacherStudentEntity);
			} else if (officeId.equals(studentDetailed.getOrgId())) {
				return new R(CommonConstants.FAIL, "该学生已于其他教师绑定", false);
			}

		}
		return new R(CommonConstants.SUCCESS, "添加成功", true);
	}
}
